package com.tms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tms.domain.LifeCycle;
import com.tms.mapper.develop.LifeCycleMapper;
import com.tms.service.LifeCycleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 生命周期记录业务实现
 *
 * @author ozj
 */
@Service
public class LifeCycleServiceImpl implements LifeCycleService {

    @Autowired
    private LifeCycleMapper lifeCycleMapper;

    @Override
    @Cacheable(cacheNames = "lifeCycles", key = "#id", unless = "#result == null")
    public LifeCycle findLifeCycleById(Integer id) {
        return lifeCycleMapper.selectByPrimaryKey(id);
    }


    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "LifeCycleInfo", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "LifeCycleList", allEntries = true, beforeInvocation = true)
            },
            put = {
                    @CachePut(cacheNames = "LifeCycles", key = "#result.id", unless = "#result == null || #result.id == 0")
            }
    )
    public LifeCycle createLifeCycle(LifeCycle lifeCycle) {
        lifeCycleMapper.insertSelective(lifeCycle);
        return lifeCycle;
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "LifeCycleInfo", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "LifeCycleList", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "lifeCycles", key = "#id")
            }
    )
    public void deleteLifeCycleById(Integer id) {
        lifeCycleMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "LifeCycleInfo", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "LifeCycleList", allEntries = true, beforeInvocation = true)
            },
            put = {
                    @CachePut(cacheNames = "lifeCycles", key = "#lifeCycle.id")
            }
    )
    public LifeCycle updateLifeCycleById(LifeCycle lifeCycle) {
        int result = lifeCycleMapper.updateByPrimaryKeySelective(lifeCycle);
        if (result == 0) {
            throw new RuntimeException("更新失败，更新结果数为0");
        }
        return lifeCycle;
    }

    @Override
    @Cacheable(cacheNames = "LifeCycleList", key = "#toolId", unless = "#result == null")
    public List<LifeCycle> findLifeCycleByCodeAndSeqId(int toolId) {
        return lifeCycleMapper.selectByToolId(toolId);
    }

    @Override
    @Cacheable(cacheNames = "LifeCycleInfo", key = "#toolId + #page + #pageSize", unless = "#result == null")
    public PageInfo<LifeCycle> findLifeCycleByPage(int toolId, int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<LifeCycle> list = lifeCycleMapper.selectByToolId(toolId);
        return new PageInfo<>(list);
    }
}